package kr.hkit.summary_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.hkit.myframework.uicomp.Utils;
import kr.hkit.summary_java.dao.DaoInterface;
import kr.hkit.summary_java.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.common.Config;
import kr.hkit.summary_java.erp.common.MenuType;
import kr.hkit.summary_java.erp.dto.CodeTitle;
import kr.hkit.summary_java.erp.dto.Department;
import kr.hkit.summary_java.erp.dto.Employee;

public class PanelListUI<T> extends JPanel {
	
	private static final long serialVersionUID = 1L;
	
	private JTable table;
	private DaoInterface<T> dao;
	private ArrayList<T> lists;
	private String[][] arLists;
	
	private String[] colNames;
	private int[] fieldSizes;
	
	public PanelListUI(MenuType type, DaoInterface<T> dao) {
		this.colNames = Config.COLNAMES[type.getValue()];
		this.fieldSizes = Config.FIELD_SIZES[type.getValue()];
		this.dao = dao;
		
		initialize();
		loadData();
		tableCellAlginAndWidth();
	}

	public void setDao(DaoInterface<T> dao) {
		this.dao = dao;
		loadData();
		tableCellAlginAndWidth();
	}

	private void initialize() {
		int width=0;
		for(int i=0; i<fieldSizes.length;i++){
			width +=fieldSizes[i];
		}
		setPreferredSize(new Dimension(width+20, 200));
		
		setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);

		table = new JTable(new String[0][0], colNames);
		table.setAutoCreateRowSorter(true);
		scrollPane.setViewportView(table);
	}
	
	public void loadData(){
		try {
			lists = dao.listItems();
			int len = lists.size();
			arLists = new String[len][colNames.length];
			for(int i=0; i<len;i++){
				T temp = lists.get(i);
				if (temp instanceof Department){
					Department d = (Department) temp;
					/*arLists[i]=new String[]{d.getDeptNo()+"", d.getDeptName(), d.getFloor()+""};*/
					arLists[i] = d.toArray();
				}else if (temp instanceof Employee){
					Employee e = (Employee) temp;
					EmployeeDAO empDao = (EmployeeDAO) dao;
					arLists[i] = new String[]{
							e.getEmpNo()+"", 
							e.getEmpName(), 
							empDao.selectedTitleById(e.getTitle()),
							e.getManager()+"",
							Utils.getComma(e.getSalary()),
							empDao.selectedDeptNameByDeptno(e.getDeptno())
							};
				}else{
					CodeTitle c = (CodeTitle) temp;
					arLists[i] = c.toArray();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			TableModel model = new DefaultTableModel(arLists, colNames);
			table.setModel(model);
		}
	}
	
	private void tableCellAlginAndWidth() {
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
				
		TableColumnModel cModel = table.getColumnModel();
		
		for(int i=0; i<colNames.length; i++){
			cModel.getColumn(i).setPreferredWidth(fieldSizes[i]);
			cModel.getColumn(i).setCellRenderer(dtcrCenter);
		}
	}


}
